package com.y.architecture.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import com.y.architecture.R;
import com.y.architecture.adapter.WeatherListAdapter;
import com.y.architecture.base_net.ApiCallback;
import com.y.architecture.base_refresh_load.BaseRefreshLoadFragment;
import com.y.architecture.dialog.TipDialog;
import com.y.architecture.recycle_util.BaseQuickAdapter;
import com.y.architecture.recycle_util.BaseViewHolder;
import com.y.architecture.util.AFConstant;
import com.y.architecture.util.AFLog;
import com.y.architecture.weather.Weather;
import com.y.architecture.weather.WeatherService;

/**
 * Created by youjingjing on 2017/11/30.
 */

public class WeatherFragment extends BaseRefreshLoadFragment<Weather.HeWeather6Bean> {
    private String title;
    private TipDialog tipsDialog;
    public WeatherFragment() {
    }

    @SuppressLint("ValidFragment")
    public WeatherFragment(String title) {
        this.title = title;
    }

    @Override
    public int getContentView() {
        return R.layout.layout_refresh_load;
    }

    @Override
    public void initViews(Bundle savedInstanceState) {
        super.initViews(savedInstanceState);
        autoRefresh();
    }

    @Override
    public String getTitleText() {
        return "";
    }

    @Override
    public BaseQuickAdapter<Weather.HeWeather6Bean, BaseViewHolder> getAdapter() {
        return new WeatherListAdapter(mItems);
    }

    @Override
    public void loadData(final int page) {
        WeatherService.getInstance().getData(title, new ApiCallback<Weather>() {
            @Override
            public void onError(Throwable e) {
                AFLog.w(e.getMessage());
            }

            @Override
            public void onNext(Weather weather) {
                if (page== AFConstant.REFRESH_FIRST_PAGE){
                    mItems.clear();
                }
                for (int i = 0; i < 15; i++) {
                    mItems.addAll(weather.getHeWeather6());
                }
                refreshLoadComplete(isSuccess(weather.getHeWeather6()));
            }
        });
        refreshComplete();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
        super.onItemClick(adapter, view, position);
        if (tipsDialog == null) {
            tipsDialog = new TipDialog(getAFContext());
        }
        tipsDialog.setTvTipsMessage("item->" + position + " clicked,is showing position").setOnLeftClickListener("确定", new TipDialog.OnLeftClickListener() {
            @Override
            public void onLeftClicked(View v) {
                showToast("item->" + position + " clicked!");
            }
        }).setOnRightClickListener("取消", null);
        tipsDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        WeatherService.getInstance().onDestory();
    }


}
